package com.ruoyi.project.jiaqin.answer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.jiaqin.answer.domain.JiaqinAnswer;
import com.ruoyi.project.jiaqin.answer.mapper.JiaqinAnswerMapper;
import com.ruoyi.project.jiaqin.answer.service.IJiaqinAnswerService;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.mapper.CompanyMapper;
import com.ruoyi.project.jiaqin.question.mapper.JiaqinQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 回答Service业务层处理
 *
 * @author admin
 * @date 2020-12-30
 */
@Service
public class JiaqinAnswerServiceImpl implements IJiaqinAnswerService
{
    @Autowired
    private JiaqinAnswerMapper jiaqinAnswerMapper;

    @Autowired
    private JiaqinQuestionMapper jiaqinQuestionMapper;

    /**
     * 查询回答
     *
     * @param id 回答ID
     * @return 回答
     */
    @Override
    public JiaqinAnswer selectJiaqinAnswerById(String id)
    {
        return jiaqinAnswerMapper.selectJiaqinAnswerById(id);
    }

    /**
     * 查询回答列表
     *
     * @param jiaqinAnswer 回答
     * @return 回答
     */
    @Override
    public List<JiaqinAnswer> selectJiaqinAnswerList(JiaqinAnswer jiaqinAnswer)
    {
        return jiaqinAnswerMapper.selectJiaqinAnswerList(jiaqinAnswer);
    }

    @Autowired
    private CompanyMapper companyMapper;
    @Override
    public PageInfo<JiaqinAnswer> selectJiaqinAnswerByQuesId(Integer pageNum, Integer pageSize, String quesId) {
        PageHelper.startPage(pageNum,pageSize);
//        JiaqinAnswer jiaqinAnswer = new JiaqinAnswer();
//        jiaqinAnswer.setQuestionId(quesId);
        List<JiaqinAnswer> list = jiaqinAnswerMapper.selectJiaqinAnswerListA(quesId);
        for (JiaqinAnswer JA : list) {
            Company company = companyMapper.selectCompanyByMemberId(JA.getMemberId());
            if (company != null) {
                JA.setIsCompany(1);
            } else {
                JA.setIsCompany(0);
            }
        }
        PageInfo<JiaqinAnswer> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增回答
     *
     * @param jiaqinAnswer 回答
     * @return 结果
     */
    @Override
    public int insertJiaqinAnswer(JiaqinAnswer jiaqinAnswer)
    {
        jiaqinAnswer.setId(UUID.randomUUID().toString().replace("-",""));
        return jiaqinAnswerMapper.insertJiaqinAnswer(jiaqinAnswer);
    }

    @Override
    @Transactional
    public int answer(JiaqinAnswer jiaqinAnswer) {
        jiaqinQuestionMapper.updateAnswers(1L,jiaqinAnswer.getQuestionId());
        jiaqinAnswer.setId(UUID.randomUUID().toString().replace("-",""));
        return jiaqinAnswerMapper.insertJiaqinAnswer(jiaqinAnswer);
    }

    /**
     * 修改回答
     *
     * @param jiaqinAnswer 回答
     * @return 结果
     */
    @Override
    public int updateJiaqinAnswer(JiaqinAnswer jiaqinAnswer)
    {
        return jiaqinAnswerMapper.updateJiaqinAnswer(jiaqinAnswer);
    }

    /**
     * 删除回答对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinAnswerByIds(String ids)
    {
        return jiaqinAnswerMapper.deleteJiaqinAnswerByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除回答信息
     *
     * @param id 回答ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinAnswerById(String id)
    {
        return jiaqinAnswerMapper.deleteJiaqinAnswerById(id);
    }
}
